/**
* @depends jquery.ui.js
*/


(function($) {

	$.widget( 'ui.toggler', {
		
		_init: function() {
		
			var $this = this;

			// Init element
			var o = this.element
				.html( this.options.messages[ this.options.currentIndex ] )
				
				
			// Bind events
				.click( function() {
					if( $.isFunction($this.options.async) ) {
						var index = ($this.options.currentIndex + 1) 
							% $this.options.messages.length;
						$this.options.async( index );
					}
					else {
						$this.toggle();
					}
				
				} );
		},
		
		toggle: function() {
			this.options.currentIndex = 
				(this.options.currentIndex + 1) 
				% this.options.messages.length;

			this.element.html( this.options.messages[ this.options.currentIndex ] );
			if( $.isFunction(this.options.toggle)) this.options.toggle();
			this._trigger( 'toggle' );
		},
		
		options: {
			messages : ['yes', 'no'],
			currentIndex : 0,
			async : false
		}
		
	} );
	
})(jQuery);
